// 从右往左遍历
var lengthOfLastWord = function(s) {
    if (s.length == 0) return 0

    let end = s.length - 1
    while (end >= 0 && s[end] == ' ') end--
    if (end < 0) return 0

    let start = end
    while (start >= 0 && s[start] != ' ') start--

    return end - start
};

// 从左往右遍历
var lengthOfLastWord = function(s) {
    let ans = 0;
    let start = 0, end = 0;
    while (end < s.length) {
        if (s.charAt(start) == ' ') {
            start++;
            end++;
        } else {
            while (end < s.length && s.charAt(end) != ' ') end++;
            ans = end - start;
            while (end < s.length && s.charAt(end) == ' ') end++;
            if (end < s.length && s.charAt(end) != ' ') {
                start = end;
            }
        }
    }
    return ans;
};